package etc.com.market;

import java.util.ArrayList;

import Controls.JsonDataHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import etc.com.sql.DBAdapter;

public class SpecifyProductDetail extends Activity implements
		OnItemClickListener {
	/** Called when the activity is first created. */
	private ListView lv;
	ArrayList<String> arrtext = new ArrayList<String>();
	int[] idimage = new int[6];
	int[] idimage1 = new int[6];
	String prodname;
	String imagename;
	String previous;
	String storename;
	String str;
	ScrollView sv;
	Bitmap[] imageIDs;
	private static final int REQUEST_CODE = 10;
	private Cursor cursor;
	private String[] relativeProdId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.specifyproductdetail);
		
		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}
		
		String productId = extras.getString("ProductId");
		String messageReceive = extras.getString("message");

		if (messageReceive != null) {
			str = messageReceive.toString();

			previous = str.substring(0, str.indexOf(";"));
			str = str.substring(str.indexOf(";") + 1);

			storename = str.substring(0, str.indexOf(";"));
			str = str.substring(str.indexOf(";") + 1);

			prodname = str.substring(0, str.indexOf(";"));
			imagename = str.substring(str.indexOf(";") + 1);
		}

		TextView v = (TextView) findViewById(R.id.storename);
		v.setText(storename);
		
//		TextView v1 = (TextView) findViewById(R.id.headerprevious);
//		v1.setText(previous);
		
		DBAdapter dbApdapter = new DBAdapter(getApplicationContext());
		cursor = dbApdapter.getProductDetailsById(productId);
		cursor.moveToNext();
		
		TextView view = (TextView) findViewById(R.id.productname);
		view.setText(cursor.getString(1));

		TextView view1 = (TextView) findViewById(R.id.btnNext);
		view1.setText(cursor.getString(1));

		ImageView ImageView = (ImageView) findViewById(R.id.productpicture);
		TextView productdesc = (TextView) findViewById(R.id.productdesc);
		productdesc.setTag(cursor.getString(4));
		
		String relativeProd = cursor.getString(7);
		relativeProdId = relativeProd.split(",");
		
		arrtext.add("糖度たっぷり今が旬");
		arrtext.add("糖度たっぷり今が旬");
		arrtext.add("糖度たっぷり今が旬");
		arrtext.add("糖度たっぷり今が旬");
		String image = "";
		String imageValue = "";
		if(cursor.getString(6) != null && !cursor.getString(6).equals("")){
			System.out.println("TEST");
			imageValue = cursor.getString(6);
		}else{
			System.out.println("TEST2");
			image = cursor.getString(8);
			if(image.substring(0, 1).equals("[")){
				image = image.substring(1, image.length() - 1);
			}
			String[] images = image.split(","); 
			String imagePath = images[0].trim().substring(1, images[0].trim().length() - 1);
			System.out.println("IMAGE PATH: " + imagePath);
			imageValue = JsonDataHelper.getImageContent(imagePath);
		}
		byte[] decodeBytes = Base64.decode(imageValue.getBytes(), Base64.DEFAULT);
		Bitmap imageBitMap = BitmapFactory.decodeByteArray(decodeBytes, 0, decodeBytes.length);
//		ImageView.setImageResource(R.drawable.product2);
			ImageView.setImageBitmap(imageBitMap);
			
			cursor.close();
			imageIDs = new Bitmap[relativeProdId.length];
			for(int i =0; i < relativeProdId.length; i++){
				Cursor cursorPath = dbApdapter.getProductDetailsById(relativeProdId[i]);
				cursorPath.moveToNext();
				String relaviceImgValue = "";
				if(cursorPath.getString(6) != null && !cursorPath.getString(6).equals("")){
					relaviceImgValue = cursorPath.getString(6);
				}else{
					String relativePath = cursorPath.getString(8);
					System.out.println(relativePath);
					if(relativePath.substring(0, 1).equals("[")){
						relativePath = relativePath.substring(1, relativePath.length() - 1);
					}
					String[] relativeImgs = relativePath.split(","); 
					String relativePathString = relativeImgs[0].trim().substring(1, relativeImgs[0].trim().length() - 1);
					relaviceImgValue = JsonDataHelper.getImageContent(relativePathString);
				}
				byte[] relativeDecode = Base64.decode(relaviceImgValue.getBytes(), Base64.DEFAULT);
				Bitmap relativeBitMap = BitmapFactory.decodeByteArray(relativeDecode, 0, relativeDecode.length);
				imageIDs[i] = relativeBitMap;
				cursorPath.close();
			}
//
//			//productdesc.setText("List Product used on Tet");
//
//			break;
//		case R.drawable.product2:
//			ImageView.setImageResource(R.drawable.product2);
//			imageIDs = new int[2];
//			imageIDs[0] = R.drawable.product3;
//			imageIDs[1] = R.drawable.product4;
//			//imageIDs[2] = R.drawable.product13;
//
//			//productdesc.setText("List Product used for Family event");
//
//			break;
//		case R.drawable.product3:
//			ImageView.setImageResource(R.drawable.product3);
//			imageIDs = new int[2];
//			imageIDs[0] = R.drawable.product1;
//			imageIDs[1] = R.drawable.product4;
//
//			break;
//		case R.drawable.product4:
//			ImageView.setImageResource(R.drawable.product4);
//			imageIDs = new int[2];
//			imageIDs[0] = R.drawable.product1;
//			imageIDs[1] = R.drawable.product3;
//
//			break;
//		case R.drawable.product5:
//			ImageView.setImageResource(R.drawable.product5);
//			imageIDs = new int[2];
//			imageIDs[0] = R.drawable.product6;
//			imageIDs[1] = R.drawable.product1;
//
//			break;
//		case R.drawable.product6:
//			ImageView.setImageResource(R.drawable.product6);
//			imageIDs = new int[2];
//			imageIDs[0] = R.drawable.product5;
//			imageIDs[1] = R.drawable.product1;
//
//			break;
//		}

		// TextView productdesc = (TextView) findViewById(R.id.productdesc);
		// productdesc.setText("product description product description product description product description product description");

		GridView gridView = (GridView) findViewById(R.id.gridview1);
		gridView.setAdapter(new ImageAdapter(this));

		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				onClickGrid(
						v,
						("List Products;" + storename + ";"
								+ arrtext.get(position) + ";" + imageIDs[position]), relativeProdId[position]);
			}
		});
		
		sv = (ScrollView) findViewById(R.id.ScrollView01);
		sv.post(new Runnable() {
			
			public void run() {
				
				sv.fullScroll(ScrollView.FOCUS_UP);
				
			}
		});
		
		cursor.close();
		
	}

	// function use for footer button ==============================
	public void Previous(View view) {
		cursor.close();
//		onClick(view, "特集一覧;" + storename);
		finish();
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void Home(View view) {
		cursor.close();
		Intent i = new Intent(this, StoreTopPage.class);
		i.putExtra("message", storename);
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void SearchProduct(View view) {
		onClickSearch(view, ("カテゴリから選択;" + storename));
	}

	public void SpecifyProduct(View view) {
		onClickSpec(view, ("特集一覧;" + storename));
	}

	public void Info(View view) {
		onClickInfo(view, storename);
	}
	
	public void Contact(View view) {
		onClickContact(view, storename);
	}

	// ===================================================================

	public void morePictures(View view) {
		cursor.close();
		Intent i = new Intent(this, moreImage.class);
		i.putExtra("message", imagename.toString());
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onImageClick(View v) {
		// TODO Auto-generated method stub
//		Intent i = new Intent(this, ImageZoomActivity.class);
//		i.putExtra("image", Integer.parseInt(imagename.toString()));
//		i.putExtra("message", "店舗詳細;" + storename);
//		// Set the request code to any code you like, you can identify the
//		// callback via this code
//		startActivity(i);
//		overridePendingTransition(R.anim.fadein,
//                R.anim.fadeout);
		return;
	}
	
	public void onClick(View view, String message) {
		cursor.close();
		Intent i = new Intent(this, ListitemActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onClickSpec(View view, String message) {
		cursor.close();
		Intent i = new Intent(this, ListitemActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onClickSearch(View view, String message) {
		cursor.close();
		Intent i = new Intent(this, SearchOursProducts.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onClickInfo(View view, String message) {
		cursor.close();
		Intent i = new Intent(this, InfomationAct.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onClickGrid(View view, String message, String refId) {
		cursor.close();
		Intent i = new Intent(this, ProductDetail.class);
		i.putExtra("message", message);
		i.putExtra("ProductID", refId);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void onClickContact(View view, String message) {
		cursor.close();
		Intent i = new Intent(this, SenmailActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void backSpecifyProduct(View view, String message) {
		cursor.close();
		Intent i = new Intent(this, ListitemActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// Toast.makeText(this, "CHON:"+arg2, Toast.LENGTH_SHORT).show();
		// Intent intent = new Intent(this, SpecifyProduct.class);
		// startActivity(intent);
	}

	public static class ViewHolder  
    {  
        public ImageView imgViewFlag;  
    }  
	
	public class ImageAdapter extends BaseAdapter {
		private Activity activity;
		public ImageAdapter(Activity c) {
			this.activity = c;
		}

		public int getCount() {
			// return arrtext.size();
			return imageIDs.length;
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		// create a new ImageView for each item referenced by the Adapter
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder view; 
			LayoutInflater inflator = activity.getLayoutInflater(); 
			if (convertView == null) {
				view = new ViewHolder();
				convertView = inflator.inflate(R.layout.gridview_row, null);
				view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
				convertView.setTag(view);  
			} else {
				view = (ViewHolder) convertView.getTag();
				
			}
			view.imgViewFlag.setImageBitmap(imageIDs[position]);
			view.imgViewFlag.setScaleType(ScaleType.FIT_XY);
			return convertView;
		}
	}
}
